package com.rogo.rogobox.models
{
	import com.rogo.mvc.sub.proxy.Proxy;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.vo.FriendsTimelineVO;
	import com.rogo.rogobox.models.vo.MentionReqVO;
	import com.rogo.rogobox.models.vo.UserTimelineVO;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	
	/**
	 * 1.1 获取下行数据集(timeline)接口
	 * 
	 * @see http://open.weibo.com/wiki/API文档
	 * 
	 * @author rogo
	 */	
	public class TimeLineProxy extends Proxy
	{
		public function TimeLineProxy()
		{
			super(NAME);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'timeLineProxy';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var blog:BlogDelegate;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		/**
		 *	获取当前登录用户及其所关注用户的最新微博消息 
		 */		
		public function loadFriendsTimeline(vo:FriendsTimelineVO):void
		{
			blog.loadFriendsTimeline(vo.sinceID, vo.maxID, vo.count, vo.page, vo.base_app, vo.feature);
		}
		
		/**
		 *	返回用户发布的最新的n条微博消息
		 */	
		public function loadUserTimeline(vo:UserTimelineVO):void
		{
			blog.loadUserTimeline(vo.id, vo.userID, vo.screenName, vo.sinceID, vo.maxID, vo.count, vo.page, vo.base_app, vo.feature);
		}
		
		/**
		 *	获取@提到我的微博消息
		 */		
		public function loadMentions(vo:MentionReqVO):void
		{
			blog.loadMentions(vo.sinceID, vo.maxID, vo.count, vo.page);
		}
		
		public function loadFavorite(page:uint):void
		{
			blog.loadFavoriteList(page);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			blog = BlogDelegate.getInstance();
			blog.addEventListener(MicroBlogEvent.LOAD_FRIENDS_TIMELINE_RESULT,
				blog_loadFriendsTimelineResultHandler);
			blog.addEventListener(MicroBlogEvent.LOAD_USER_TIMELINE_RESULT,
				blog_loadUserTimelineResultHandler);
			blog.addEventListener(MicroBlogEvent.LOAD_MENSIONS_RESULT,
				blog_loadMentionsResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.LOAD_MENSIONS_ERROR,
				blog_loadMentionsErrorHandler);
			blog.addEventListener(MicroBlogEvent.LOAD_FAVORITE_LIST_RESULT,
				blog_loadFavoriteResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.LOAD_FAVORITE_LIST_ERROR,
				blog_loadFavoriteErrorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function blog_loadFriendsTimelineResultHandler(e:MicroBlogEvent):void
		{
			var result:Array = e.result as Array;
			if (!result || result.length == 0) 
				return;
			
			sendNotification(AppFacade.FRIENDS_TIMELINE_RESULT, result);
		}
		
		private function blog_loadUserTimelineResultHandler(e:MicroBlogEvent):void
		{
			var result:Array = e.result as Array;
			if (!result || result.length == 0) 
				return;
			
			sendNotification(AppFacade.LOAD_USER_TIMELINE_RESULT, result);
		}
		
		private function blog_loadMentionsResultHandler(e:MicroBlogEvent):void
		{
			var result:Array = e.result as Array;
			if (!result || result.length == 0)
				return;
			
			sendNotification(AppFacade.LOAD_MENTION_RESULT, result);
		}
		
		private function blog_loadMentionsErrorHandler(e:MicroBlogErrorEvent):void
		{}
		
		private function blog_loadFavoriteResultHandler(e:MicroBlogEvent):void
		{
			var result:Array = e.result as Array;
			if (!result || result.length == 0)
				return;
			
			sendNotification(AppFacade.LOAD_FAVORITE_RESULT, result);
		}
		
		private function blog_loadFavoriteErrorHandler(e:MicroBlogErrorEvent):void
		{}
		
	}
}